﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:ProductInfoDal
    /// </summary>
    public partial class ProductInfoDal : IProductInfo
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public ProductInfoDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string productName, int productId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_PRODUCT_INFO");
            strSql.Append(" where PRODUCT_NAME=@PRODUCT_NAME and PRODUCT_ID not in (@PRODUCT_ID)");
            SqlParameter[] parameters = {
					new SqlParameter("@PRODUCT_NAME", SqlDbType.VarChar,100),
                    new SqlParameter("@PRODUCT_ID", SqlDbType.Int,4)
};
            parameters[0].Value = productName;
            parameters[1].Value = productId;
            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), parameters).ToString()) > 0;

        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ProductInfoModel model,string typeIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"declare @num int;select @num=max(ORDER_NUM)from COFCO_PRODUCT_INFO where PRODUCT_BRAND_ID in (" + typeIdlist + ");");
            strSql.Append("insert into COFCO_PRODUCT_INFO(");
            strSql.Append("PRODUCT_NAME,PRODUCT_TYPE_ID,PRODUCT_BRAND_ID,PRODUCT_DESC,PRODUCT_IMG,ORDER_NUM,BUY_LINK,STATE,ADD_TIME,ADD_USERNAME)");
            strSql.Append(" values (");
            strSql.Append("@PRODUCT_NAME,@PRODUCT_TYPE_ID,@PRODUCT_BRAND_ID,@PRODUCT_DESC,@PRODUCT_IMG,@num+1,@BUY_LINK,@STATE,@ADD_TIME,@ADD_USERNAME)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@PRODUCT_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@PRODUCT_TYPE_ID", SqlDbType.Int,4),
					new SqlParameter("@PRODUCT_BRAND_ID", SqlDbType.Int,4),
					new SqlParameter("@PRODUCT_DESC", SqlDbType.VarChar,800),
					new SqlParameter("@PRODUCT_IMG", SqlDbType.VarChar,50),					
					new SqlParameter("@BUY_LINK", SqlDbType.VarChar,500),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ADD_TIME", SqlDbType.DateTime),
					new SqlParameter("@ADD_USERNAME", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Productname;
            parameters[1].Value = model.Producttypeid;
            parameters[2].Value = model.Productbrandid;
            parameters[3].Value = model.Productdesc;
            parameters[4].Value = model.Productimg;           
            parameters[5].Value = model.Buylink;
            parameters[6].Value = model.State;
            parameters[7].Value = model.Addtime;
            parameters[8].Value = model.Addusername;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ProductInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_PRODUCT_INFO set ");
            strSql.Append("PRODUCT_NAME=@PRODUCT_NAME,");
            strSql.Append("PRODUCT_TYPE_ID=@PRODUCT_TYPE_ID,");
            strSql.Append("PRODUCT_BRAND_ID=@PRODUCT_BRAND_ID,");
            strSql.Append("PRODUCT_DESC=@PRODUCT_DESC,");
            strSql.Append("PRODUCT_IMG=@PRODUCT_IMG,");
            strSql.Append("ORDER_NUM=@ORDER_NUM,");
            strSql.Append("BUY_LINK=@BUY_LINK,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("ADD_TIME=@ADD_TIME,");
            strSql.Append("ADD_USERNAME=@ADD_USERNAME");
            strSql.Append(" where PRODUCT_ID=@PRODUCT_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@PRODUCT_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@PRODUCT_TYPE_ID", SqlDbType.Int,4),
					new SqlParameter("@PRODUCT_BRAND_ID", SqlDbType.Int,4),
					new SqlParameter("@PRODUCT_DESC", SqlDbType.VarChar,800),
					new SqlParameter("@PRODUCT_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@ORDER_NUM", SqlDbType.Int,4),
					new SqlParameter("@BUY_LINK", SqlDbType.VarChar,500),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ADD_TIME", SqlDbType.DateTime),
					new SqlParameter("@ADD_USERNAME", SqlDbType.VarChar,50),
					new SqlParameter("@PRODUCT_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Productname;
            parameters[1].Value = model.Producttypeid;
            parameters[2].Value = model.Productbrandid;
            parameters[3].Value = model.Productdesc;
            parameters[4].Value = model.Productimg;
            parameters[5].Value = model.Ordernum;
            parameters[6].Value = model.Buylink;
            parameters[7].Value = model.State;
            parameters[8].Value = model.Addtime;
            parameters[9].Value = model.Addusername;
            parameters[10].Value = model.Productid;

            int rows = SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PRODUCT_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_PRODUCT_INFO ");
            strSql.Append(" where PRODUCT_ID=@PRODUCT_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@PRODUCT_ID", SqlDbType.Int,4)
};
            parameters[0].Value = PRODUCT_ID;

            int rows = SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string PRODUCT_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_PRODUCT_INFO ");
            strSql.Append(" where PRODUCT_ID in (" + PRODUCT_IDlist + ")  ");
            int rows = SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ProductInfoModel GetModel(int PRODUCT_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PRODUCT_ID,PRODUCT_NAME,PRODUCT_TYPE_ID,PRODUCT_BRAND_ID,PRODUCT_DESC,PRODUCT_IMG,ORDER_NUM,BUY_LINK,STATE,ADD_TIME,ADD_USERNAME from COFCO_PRODUCT_INFO ");
            strSql.Append(" where PRODUCT_ID=" + PRODUCT_ID);
            ProductInfoModel model = new ProductInfoModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PRODUCT_ID"].ToString() != "")
                {
                    model.Productid = int.Parse(ds.Tables[0].Rows[0]["PRODUCT_ID"].ToString());
                }
                model.Productname = ds.Tables[0].Rows[0]["PRODUCT_NAME"].ToString();
                if (ds.Tables[0].Rows[0]["PRODUCT_TYPE_ID"].ToString() != "")
                {
                    model.Producttypeid = int.Parse(ds.Tables[0].Rows[0]["PRODUCT_TYPE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PRODUCT_BRAND_ID"].ToString() != "")
                {
                    model.Productbrandid = int.Parse(ds.Tables[0].Rows[0]["PRODUCT_BRAND_ID"].ToString());
                }
                model.Productdesc = ds.Tables[0].Rows[0]["PRODUCT_DESC"].ToString();
                model.Productimg = ds.Tables[0].Rows[0]["PRODUCT_IMG"].ToString();
                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.Ordernum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                model.Buylink = ds.Tables[0].Rows[0]["BUY_LINK"].ToString();
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADD_TIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADD_TIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADD_USERNAME"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ProductInfoModel GetModelByOrderNum(int OrderNum,int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PRODUCT_ID,PRODUCT_NAME,PRODUCT_TYPE_ID,PRODUCT_BRAND_ID,PRODUCT_DESC,PRODUCT_IMG,ORDER_NUM,BUY_LINK,STATE,ADD_TIME,ADD_USERNAME from COFCO_PRODUCT_INFO ");
            strSql.Append(" where ORDER_NUM=" + OrderNum + " and PRODUCT_BRAND_ID="+id);
            ProductInfoModel model = new ProductInfoModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PRODUCT_ID"].ToString() != "")
                {
                    model.Productid = int.Parse(ds.Tables[0].Rows[0]["PRODUCT_ID"].ToString());
                }
                model.Productname = ds.Tables[0].Rows[0]["PRODUCT_NAME"].ToString();
                if (ds.Tables[0].Rows[0]["PRODUCT_TYPE_ID"].ToString() != "")
                {
                    model.Producttypeid = int.Parse(ds.Tables[0].Rows[0]["PRODUCT_TYPE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PRODUCT_BRAND_ID"].ToString() != "")
                {
                    model.Productbrandid = int.Parse(ds.Tables[0].Rows[0]["PRODUCT_BRAND_ID"].ToString());
                }
                model.Productdesc = ds.Tables[0].Rows[0]["PRODUCT_DESC"].ToString();
                model.Productimg = ds.Tables[0].Rows[0]["PRODUCT_IMG"].ToString();
                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.Ordernum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                model.Buylink = ds.Tables[0].Rows[0]["BUY_LINK"].ToString();
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADD_TIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADD_TIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADD_USERNAME"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PRODUCT_ID,PRODUCT_NAME,PRODUCT_TYPE_ID,PRODUCT_BRAND_ID,PRODUCT_DESC,PRODUCT_IMG,ORDER_NUM,BUY_LINK,STATE,ADD_TIME,ADD_USERNAME ");
            strSql.Append(" FROM COFCO_PRODUCT_INFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by ORDER_NUM");
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PRODUCT_ID,PRODUCT_NAME,PRODUCT_TYPE_ID,PRODUCT_BRAND_ID,PRODUCT_DESC,PRODUCT_IMG,ORDER_NUM,BUY_LINK,STATE,ADD_TIME,ADD_USERNAME ");
            strSql.Append(" FROM COFCO_PRODUCT_INFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_PRODUCT_INFO";
            parameters[1].Value = "PRODUCT_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

